package com.tgy.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tgy.admin.Service.PermissionService;
import com.tgy.dTree.Dtree;
import com.tgy.page.PageQuery;
import com.tgy.pojo.SysPermission;
import com.tgy.result.GraceJSONResult;
import com.tgy.result.LayuiTableResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/6/26 10:36
 */
@Validated
@Api(value = "后台权限控制器", tags = {"后台权限管理"})
@RestController
@RequestMapping("/admin/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @ApiOperation("获取后台权限列表")
    // @SaCheckPermission
    @GetMapping("getSysPermissionList")
    public LayuiTableResult getSysPermissionList(SysPermission bo, PageQuery pageQuery){
        return permissionService.getSysPermissionList(bo,pageQuery);
    }

    @ApiOperation("修改后台权限状态")
    @SaCheckPermission("syspermission:update")
    @PostMapping("updateStatus")
    public GraceJSONResult updateStatus(SysPermission sysPermission){
        return permissionService.updateStatus(sysPermission.getId(),sysPermission.getAvailable());
    }

    @ApiOperation("添加后台权限")
    @SaCheckPermission("syspermission:add")
    @PostMapping("addSysPermission")
    public GraceJSONResult addSysPermission(SysPermission sysPermission){
        return permissionService.addSysPermission(sysPermission);
    }

    @ApiOperation("修改后台权限")
    @SaCheckPermission("syspermission:update")
    @PostMapping("updateSysPermission")
    public GraceJSONResult updateSysPermission(SysPermission sysPermission){
        return permissionService.updateSysPermission(sysPermission);
    }

    @ApiOperation("删除后台权限")
    @SaCheckPermission("syspermission:del")
    @PostMapping("delSysPermission")
    // @PostMapping("delSysPermission/{id}")
    public GraceJSONResult delSysPermission(@ApiParam("ID必须传入") String id){
        return permissionService.delSysPermission(id);
    }

    @ApiOperation("批量删除后台权限")
    @SaCheckPermission("syspermission:del")
    @PostMapping("delSysPermissionIds")
    public GraceJSONResult delSysPermissionIds(@ApiParam("ids必须传入") String ids){
        List<String> list = new ArrayList<>(Arrays.asList(ids.split(",")));
        return permissionService.delSysPermissionIds(list);
    }

    @ApiOperation("根据ID查询后台权限")
    @SaCheckPermission
    @PostMapping("getSysPermissionById")
    public GraceJSONResult getSysPermissionById(@ApiParam("ID必须传入") String id){
        return permissionService.getSysPermissionById(id);
    }

    @ApiOperation("查询父级菜单下拉树，只返回状态为启用的，此处返回格式必须以children的格式返回")
    // @SaCheckPermission
    @GetMapping("getSysPermission")
    public Dtree getSysPermission(){
        return permissionService.getSysPermission();
    }
    

}
